package com.doctor.comm;

import android.app.Application;

import com.doctor.bean.UserBean;
import com.doctor.constants.Constants;
import com.doctor.database.DbOperator;
import com.doctor.ui.wxapi.MyWXApi;
import com.doctor.utils.crash.AppCrashHandler;
import com.doctor.utils.logutils.CrashHandler;
import com.doctor.utils.logutils.LogUtils;
import com.doctor.utils.network.NetWorkReceiverUtils;

public class MyApplication extends Application{
	
	/**
	 * application用来存储用户信息
	 */
	
	private static MyApplication mInstance;
	private int alarmCount=-1;
	
	private UserBean userBean;
	//是否已通知用户有新的数据
	private boolean hasNoticeNewData = false;
	
	

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		mInstance = this;
		init();//初始化
		//================上线时需要设置为true============================
		isFormalVersion(Constants.FORMAL);//测试服务器是false,正式服务器是true
		//==============================================================


	}

	public int getAlarmCount() {
		return alarmCount;
	}

	public void setAlarmCount(int alarmCount) {
		this.alarmCount = alarmCount;
	}

	public int getUserId() {
		return getUserBean().getId();
	}

	public String getLocalName() {
		return getUserBean().getLocalName();
	}

	public String getLocalPartment() {
		return getUserBean().getLocalPartment();
	}

	public String getLocalHospital() {
		return getUserBean().getLocalHospital();
	}

	public String getLocalPhoto() {
		return getUserBean().getLocalPhoto();
	}

	public String getDeviceId() {
		return getUserBean().getSerial_number();
	}
	
	
	public UserBean getUserBean(){
		if (userBean==null) {
			userBean=DbOperator.getInstance(this).selectUserInfo();
		}
		return userBean;
	}
	
	/**
	 * 刷新用户数据
	 */
	public void freshUser() {
		userBean = DbOperator.getInstance(this).selectUserInfo();
	} 
	
	public void setUserBean(UserBean userBean){
		this.userBean=userBean;
	}
	
	public boolean getHasNoticeNewData() {
		return hasNoticeNewData;
	}
	
	public void setHasNoticeNewData(boolean hasNoticeNewData) {
		this.hasNoticeNewData = hasNoticeNewData;
	}


	/**
	 * 初始化tools
	 */
	private void init() {
		//初始化网络监听器
		NetWorkReceiverUtils.getInstance().registerNetWorkReceiver(this);
		//初始化CrashReport系统
		initCrashReport();
		//初始化环信配置
//		initHX();
//		if (inMainProcess()) {
//			StorageUtil.init(this, null);//初始化存储工具
//		}
		//注册微信
		MyWXApi.regToWx(mInstance);
	}


	/**
	 * 初始化CrashReport系统
	 */
	private void initCrashReport() {
		CrashHandler crashHandler = CrashHandler.getInstance();
		crashHandler.init(getInstance());
		AppCrashHandler.getInstance(getInstance());
	}

	/**
	 * 判断是否是正式服务器的方法
	 *
	 * @param isFormal 是否是正式服务器
	 */
	private void isFormalVersion(boolean isFormal) {
		if (isFormal) {
			LogUtils.close();//如果是正式上线版本则关闭log
		} else {
			LogUtils.openLog();//测试版本则开启log
		}
	}

	public static MyApplication getInstance() {
		if (mInstance == null) {
			mInstance = new MyApplication();
		}
		return mInstance;
	}
	
}
